package com.handels.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.handels.core.HAApplication;
import com.handels.util.HWAUtility;

public class ConnectionManager {
    private static final String STR_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//    private static final String NOME_BANCO = "CPFLDBTST";
    private static final String NOME_BANCO = "HND_CPFL";

    public static String banco;

    // ------------------------------------------------------------------------------

    public static Connection getConexao() {
        Connection conn = null;
        try {
            Class.forName(STR_DRIVER);

            // ** pega acesso ao banco do config.xml
            String Realpath = HAApplication.getInstance().getRealPath();
            HWAUtility utility = new HWAUtility();
            banco = utility.getBanco(Realpath);
            conn = DriverManager.getConnection(banco);

            System.out.println("ConnectionManager SUCESSO " + conn);
            // conn = DriverManager.getConnection(banco);
        } catch (Exception e) {
            System.out.println("ERROR ConnectionManager exception " + e);
            System.out.println("ERROR ConnectionManager banco " + banco);
            e.printStackTrace();

            throw new RuntimeException(e);
        }

        return conn;
    }

    // ------------------------------------------------------------------------------

    public static void close(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // ------------------------------------------------------------------------------

}

